package com.leyou.auth.web;

import com.leyou.auth.dto.UserDetail;
import com.leyou.auth.service.UserAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("user")
public class UserAuthController {

    @Autowired
    private UserAuthService userAuthService;

    /**
     * 登录接口
     * @param username
     * @param password
     * @param response
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity<Void>login(
          @RequestParam("username") String username ,
            @RequestParam("password") String password,
            HttpServletResponse response
    ){
        //调用service 完成登录
        this.userAuthService.login(username,password,response);
//登录成功 无返回值  204状态码
        return ResponseEntity.noContent().build();
    }
  @PostMapping("/logout")
    public ResponseEntity<Void>logout(
            @CookieValue("LY_TOKEN") String token,HttpServletResponse response){
         this.userAuthService.logout(token,response);
         return ResponseEntity.ok().build();
    }

    @GetMapping("/info/me")
    public ResponseEntity<UserDetail>verifyUser(@CookieValue("LY_TOKEN")String token){
        return ResponseEntity.ok(this.userAuthService.verifyUser(token));
    }
}
